package com.peng.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.pagehelper.PageInfo;
import com.peng.constant.Constants;
import com.peng.mapper.TActivityRemarkMapper;
import com.peng.model.TActivityRemark;
import com.peng.model.TUser;
import com.peng.query.ActivityRemarkQuery;
import com.peng.query.BaseQuery;
import com.peng.result.R;
import com.peng.service.TActivityRemarkService;
import com.peng.util.JWTUtils;
import org.apache.poi.hssf.record.DVALRecord;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;

@RestController
public class ActivityRemarkController {
    @Autowired
    TActivityRemarkService tActivityRemarkService;
    @Autowired
    TActivityRemarkMapper tActivityRemarkMapper;

    //提交新增的备注
    @PostMapping(value = "/api/activity/remark")
    public R addActivityRemark(@ModelAttribute ActivityRemarkQuery activityRemarkQuery, @RequestHeader(value = "Authorization") String token) {
        //axios提交post请求，提交过来的是json数据，使用@RequestBody注解接收
        activityRemarkQuery.setToken(token);
        int save = tActivityRemarkService.saveActivityRemark(activityRemarkQuery);
        return save >= 1 ? R.OK( ) : R.FAIL();
    }

    //分页获取当前市场活动的备注
    @GetMapping(value = "/api/activity/remark")
    public R activityRemarkPage(@RequestParam(value = "current", required = false) Integer current,
                                @RequestParam(value = "activity_id") Integer activity_id) {

        ActivityRemarkQuery activityRemarkQuery = new ActivityRemarkQuery();
        activityRemarkQuery.setActivity_id(activity_id);

        if (current == null) {current = 1;}

        IPage<TActivityRemark> TActivityRemarkIPage = tActivityRemarkMapper
                .selecTActivityRemarkIPage(activityRemarkQuery,new Page<TActivityRemark>(current, Constants.PAGE_SIZE));
        return R.OK(TActivityRemarkIPage);
    }

    //编辑时获取 被编辑备注 的信息
    @GetMapping(value = "/api/activity/remark/{id}")
    public R activityRemarkPage(@PathVariable(value = "id") Integer id) {
        TActivityRemark tActivityRemark = tActivityRemarkService.getById(id);
        return R.OK(tActivityRemark);
    }

    //编辑备注的提交
    @PutMapping(value = "/api/activity/remark")
    public R activityRemarkPage(@ModelAttribute ActivityRemarkQuery activityRemarkQuery,@RequestHeader(value = "Authorization") String token) {

        TActivityRemark tActivityRemark = new TActivityRemark();
        //备注id
        tActivityRemark.setId(activityRemarkQuery.getId());
        //备注信息更改
        tActivityRemark.setNote_content(activityRemarkQuery.getNote_content());
        //编辑时间
        tActivityRemark.setEdit_time(new Date());
        //登录人的id
        Integer loginUserId = JWTUtils.parseUserFromJWT(token).getId();
        //编辑人
        tActivityRemark.setEdit_by(loginUserId);


        boolean updata = tActivityRemarkService.updateById(tActivityRemark);
        return updata ? R.OK() : R.FAIL();
    }
}
